package com.gmss.crawler.task;

import com.alibaba.fastjson.JSONObject;
import com.gmss.crawler.module.person.service.IPersonInfoService;
import com.gmss.crawler.module.title.service.IPersonTitleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 维基百科-导步爬取
 */
@Slf4j
@Service
public class WikiTask {

    @Resource
    private IPersonTitleService iPersonTitleService;
    @Resource
    private IPersonInfoService iPersonInfoService;

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1024,
            1024, 0L, TimeUnit.MILLISECONDS,
            new ArrayBlockingQueue<>(1024), new ThreadPoolFactory("WikiTask-data-task"));


    public void submit(JSONObject requestJson, Object object, Long cost){
        threadPoolExecutor.submit(new SyncDataThread(requestJson, object, cost));
    }

    private class SyncDataThread implements Runnable{
        // 入参
        private JSONObject requestJson;
        // 出参
        private Object object;
        // 耗时
        private Long cost;
        public SyncDataThread(JSONObject requestJson, Object object, Long cost){
            this.requestJson   = requestJson;
            this.object  = object;
            this.cost  = cost;
        }
        @Override
        public void run() {
            //保存
            try {
            } catch (Exception e) {
                log.info("WikiTask异常：request:[{}], Exception:[{}]", requestJson, e);

            }
        }
    }

    @PreDestroy
    public void destroy() {
        if (null != this.threadPoolExecutor) {
            this.threadPoolExecutor.shutdown();
        }
    }
}
